package com.haizhi.crm.recommend.service.imp;

import com.haizhi.crm.biz.dao.BrowseCountDao;
import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.util.DateUtils;
import com.haizhi.crm.recommend.constant.RecType;
import com.haizhi.crm.recommend.model.RecQo;
import com.haizhi.crm.recommend.model.RecVo;
import com.haizhi.crm.recommend.service.RecService;
import com.haizhi.crm.search.builder.SearchQoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yangyijun on 2018/7/6.
 */
@Service
public class OuterEventHotSearch extends BaseRecService implements RecService {

    public static final GLog LOG = new GLog(OuterEventHotSearch.class);

    @Autowired
    private BrowseCountDao browseCountDao;

    @Override
    public String getName() {
        return RecType.OuterEvent.SEARCH_HOT_SEARCH;
    }

    @Override
    public RecVo doRecommend(RecQo recQo) {
        try {
            Date startDate = DateUtils.getDaysBefore(new Date(), 7);
            String index = recQo.getGraph();
            List<String> entityKeys = browseCountDao.findEntityKeysBy(CrmSchema.EVENT_OUTTER, startDate);

            SearchQoBuilder searchQoBuilder = new SearchQoBuilder();
            searchQoBuilder.setGraph(index);
            searchQoBuilder.addSchemas(CrmSchema.EVENT_OUTTER);
            searchQoBuilder.addTermFilter("object_key", entityKeys.toArray());
            searchQoBuilder.addSort("event_time", "desc" );
            Object data = searchService.search(searchQoBuilder.get()).getSearchResp().getData();
            return (RecVo) new RecVo().setPayload(data);
        } catch (Exception e) {
            LOG.error("recommend outer event hot search error!", e);
            return (RecVo) new RecVo().setPayload(new ArrayList<>());
        }
    }
}
